public with sharing class AccountContactRelationshipTriggerHandler {
    
public void OnAfterContactInsertRelationship(List<Contact> contact){
           List<Relationship__c> relationshipList=new List<Relationship__c>();
   if(contact !=null && contact.size()>0){ 
    for(Contact con:contact){
           Relationship__c rel=new Relationship__c();
                rel.Account__c=con.AccountId;
                rel.Contact__c=con.Id;
                rel.Start_Date__c=Date.today();
                rel.Status__c=con.Status__c;
                rel.Affiliate_Organizational_Role__c=con.Affiliate_Organizational_Role__c;
           relationshipList.add(rel); 
    }
    if(!relationshipList.isEmpty()){
         try{ 
          insert relationshipList;
          }catch(Exception e1){system.debug('## Exception while inserting : '+e1);}
       }
    
   }
}
public void OnAfterContactUpdateRelationship(List<Contact> contact){
            Set<Id> accountId=new Set<Id>();
            Set<Id> contactId=new Set<Id>();
            List<String> affiliaterole=new List<String>();
            List<Relationship__c> relationshipInsertList=new List<Relationship__c>();
            List<Relationship__c> relationshipList=new List<Relationship__c>();
           
            
     if(contact !=null && contact.size()>0){             
           for(Contact con:contact){
               accountId.add(con.AccountId);
                contactId.add(con.Id);
                affiliaterole.add(con.Affiliate_Organizational_Role__c);
             } 
         
          relationshipList=[select Id from Relationship__c where contact__c IN: contactId and account__c IN:accountId and Affiliate_Organizational_Role__c=:affiliaterole and Id !=null];
      
    for(Contact con:contact){
           
            if(relationshipList!=null && relationshipList.size()>0){
             //Trigger.new[0].addError('Relationship already exist');    
             system.debug('Relationship already exist');    
            }
            else{
                Relationship__c rel=new Relationship__c();
                rel.Account__c=con.AccountId;
                rel.Contact__c=con.Id;
                rel.Affiliate_Organizational_Role__c=con.Affiliate_Organizational_Role__c;
                rel.Start_Date__c=Date.today();
                relationshipInsertList.add(rel);
                //insert rel;
            }
  
    }
   if(!relationshipInsertList.isEmpty()){
         try{ 
          insert relationshipInsertList;
          }catch(Exception e1){system.debug('## Exception while inserting : '+e1);}
       } 
 }
}
public void OnbeforeRelationshipDelete(Map<id,Relationship__c> relationships){
     Set<Id> realtionshipids=new  Set<Id> ();
    if(relationships != null && relationships.size()>0){       
        for(Relationship__c rel: relationships.values()){
            realtionshipids.add(rel.id);          
        }
  }
  List<Relationship__c> realtionsgiplist=[select id,Contact__r.AccountId,account__c from Relationship__c where id In:realtionshipids];
  
          for(Relationship__c rel: realtionsgiplist){
  
               if(rel.account__c==rel.contact__r.AccountId){
                   Trigger.oldmap.get(rel.id).addError('Cannot delete Contact from relationship');
                     //rel.Id.addError('Cannot delete Contact from relationship');
                    //system.debug('Cannot delete Contact from relationship');
                }
              }
               
        }
public void OnBeforeRelationshipInsert(List<Relationship__c> relationshipList){
    Set<Id> accountId=new Set<Id>();
    Set<Id> contactId=new Set<Id>();
    Set<Id> relationshipId=new Set<Id>();
    Set<String> relationshiptype=new Set<String>();
    List<Relationship__c> relationship=new List<Relationship__c>();
    List<Relationship__c> relationshipInsertList=new List<Relationship__c>();
     List<Relationship__c> relationshipExistList=new List<Relationship__c>();
if(relationshipList != null && relationshipList.size()>0){
        for(Relationship__c rel: relationshipList){
                accountId.add(rel.account__c);
                contactId.add(rel.contact__c);
                relationshipId.add(rel.Id);
                relationshiptype.add(rel.Affiliate_Organizational_Role__c);
                if(rel.End_Date__c != null){
                    rel.Status__c='Inactive';
                }
        }  
      relationship=[select Id,account__c ,contact__c ,Affiliate_Organizational_Role__c from Relationship__c where account__c IN: accountId and contact__c IN: contactId and Id not IN: relationshipId and Affiliate_Organizational_Role__c IN:relationshiptype and Status__c='Active'];
      
      if(relationship!=null && relationship.size()>0){
        for(Relationship__c r: relationshipList){  
          if(!r.isExist__c){
              Trigger.new[0].addError('Relationship already exist');  
          }else{
           for(Relationship__c r1: relationship){ 
            if(r.account__c != r1.account__c && r.contact__c != r1.contact__c && r.Affiliate_Organizational_Role__c != r1.Affiliate_Organizational_Role__c){
                relationshipInsertList.add(r);
              }
           }
          }
          
        }
  } 
  if(relationshipInsertList!=null && relationshipInsertList.size()>0){
    insert relationshipInsertList;
    }              
}
}
/************************************************************************
        Purpose     : Test Class                  
        Parameters  : None
        Returns     : None
***********************************************************************/
    @isTest(SeeAllData=false)
     static public void testContactAddressTriggerHandler() {
         PIMS_Reserved_Id__c pimsid=new PIMS_Reserved_Id__c();
         pimsid.PIMS_Id__c='0000-0000';
         pimsid.Used__c=false;
         insert pimsid;
         
         PIMS_Reserved_Id__c pimsid1=new PIMS_Reserved_Id__c();
         pimsid1.PIMS_Id__c='0001-0000';
         pimsid1.Used__c=false;
         insert pimsid1;
         
         PIMS_Reserved_Id__c pimsid2=new PIMS_Reserved_Id__c();
         pimsid2.PIMS_Id__c='0002-0000';
         pimsid2.Used__c=false;
         insert pimsid2;
         
         Account acc=new Account();
         acc.Name='test';
         acc.Type='SSO';
         insert acc;
         
         Account acc1=new Account();
         acc1.Name='test1';
         acc1.Type='SSOO';
         insert acc1;
         
         
         Contact con=new Contact();
         con.LastName='Test';
         con.AccountId=acc.id;
         con.Status__c='Active';
         con.Affiliate_Organizational_Role__c='test';
         con.Gender__c='Male';
         insert con;
         
         con.AccountId=acc1.id;
         con.Affiliate_Organizational_Role__c='test1';
         update con;
         
         Relationship__c rel=new Relationship__c();
                rel.Account__c=acc.Id;
                rel.Contact__c=con.Id;
                rel.Start_Date__c=Date.today();
                rel.Status__c=con.Status__c;
                rel.Affiliate_Organizational_Role__c='test2';
                insert rel;
                update rel;
                delete rel;
         
         
     }

}